package mac10.monitor;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JComponent;

import mac10.models.Cell;
import mac10.models.WorldModel;

public class WorldComponent extends JComponent
{
    
	private static final long serialVersionUID = 6593549516849377970L;
	private CellComponent[][] world;
    private GridLayout layout;
    
    private BufferedImage agent_blue, agent_red, agent_red_dark, cow,
        cow_dark, fence, fence_dark, grass, grass_dark, stable1,
        stable1_dark, stable2, stable2_dark, lever, lever_dark, tree,
        tree_dark, blank;
    
    
    
    public WorldComponent(WorldModel worldModel)
    {
        int width = worldModel.getWidth();
        int height = worldModel.getHeight();
        this.world = new CellComponent[width][height];
        this.layout = new GridLayout(height, width);
        this.setLayout(layout);
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                this.world[j][i] = new CellComponent(worldModel.getCell(j, i));
                this.add(world[j][i]);
            }
        }
        this.setMinimumSize(layout.minimumLayoutSize(this));
        this.readImages();
    }
    
    public void updateWorld(WorldModel worldModel)
    {
        int currStep = worldModel.getCurrStep();
        Color color;
        BufferedImage image;
        for (int x = 0; x < world.length; x++) {
            for (int y = 0; y < world[0].length; y++) {
                Cell cell = worldModel.getCell(x, y);
                if (cell.isUnknown())
                {
                    color = CellComponent.UNKNOWN;
                    image = blank;
                }
                else if (cell.isCow())
                {
                    if (cell.getTimestamp() == currStep)
                    {
                        color = CellComponent.COW;
                        image = cow;
                    }
                    else
                    {
                        color = CellComponent.COW_DARK;
                        image = cow_dark;
                    }
                }
                else if (cell.isAllyAgent())
                {
                    color = CellComponent.ALLY_AGENT;
                    image = agent_blue;
                }
                else if (cell.isEnemyAgent())
                {
                    if (cell.getTimestamp() == currStep)
                    {
                        color = CellComponent.ENEMY_AGENT;
                        image = agent_red;
                    }
                    else
                    {
                        color = CellComponent.ENEMY_AGENT_DARK;
                        image = agent_red_dark;
                    }
                }
                else if (cell.isObstacle())
                {
                    if (cell.getTimestamp() == currStep)
                    {
                        color = CellComponent.OBSTACLE;
                        image = tree;
                    }
                    else
                    {
                        color = CellComponent.OBSTACLE_DARK;
                        image = tree_dark;
                    }
                }
                else if (cell.isAllyCorral())
                {
                    if (cell.getTimestamp() == currStep)
                    {
                        color = CellComponent.ALLY_CORRAL;
                        image = stable1;
                    }
                    else
                    {
                        color = CellComponent.ALLY_CORRAL_DARK;
                        image = stable1_dark;
                    }
                }
                else if (cell.isEnemyCorral())
                {
                    if (cell.getTimestamp() == currStep)
                    {
                        color = CellComponent.ENEMY_CORRAL;
                        image = stable2;
                    }
                    else
                    {
                        color = CellComponent.ENEMY_CORRAL_DARK;
                        image = stable2_dark;
                    }
                }
                else if (cell.isFence() && !cell.isOpen())
                {
                    if (cell.getTimestamp() == currStep)
                    {
                        color = CellComponent.CLOSED_FENCE;
                        image = fence;
                    }
                    else
                    {
                        color = CellComponent.CLOSED_FENCE_DARK;
                        image = fence_dark;
                    }
                }
                else if (cell.isSwitch())
                {
                    if (cell.getTimestamp() == currStep)
                    {
                        color = CellComponent.SWITCH;
                        image = lever;
                    }
                    else
                    {
                        color = CellComponent.SWITCH_DARK;
                        image = lever_dark;
                    }
                }
                else if (cell.getTimestamp() == currStep)
                {
                    color = CellComponent.EMPTY;
                    image = grass;
                }
                else
                {
                    color = CellComponent.EMPTY_DARK;
                    image = grass_dark;
                }

                world[x][y].setCellColor(color);
                world[x][y].setCellImage(image);
                world[x][y].setCell(cell);
            }
        }
    }
    
    private void readImages() {
        try {
            agent_blue = ImageIO.read(new File(
                    "src/main/resources/img/agent_blue.png"));
            agent_red = ImageIO.read(new File(
                    "src/main/resources/img/agent_red.png"));
            agent_red_dark = ImageIO.read(new File(
                    "src/main/resources/img/agent_red_dark.png"));
            cow = ImageIO.read(new File(
                    "src/main/resources/img/cow.png"));
            cow_dark = ImageIO.read(new File(
                    "src/main/resources/img/cow_dark.png"));
            fence = ImageIO.read(new File(
                    "src/main/resources/img/fence.png"));
            fence_dark = ImageIO.read(new File(
                    "src/main/resources/img/fence_dark.png"));
            grass = ImageIO.read(new File(
                    "src/main/resources/img/grass.png"));
            grass_dark = ImageIO.read(new File(
                    "src/main/resources/img/grass_dark.png"));
            stable1 = ImageIO.read(new File(
                    "src/main/resources/img/Stable1.png"));
            stable1_dark = ImageIO.read(new File(
                    "src/main/resources/img/Stable1_dark.png"));
            stable2 = ImageIO.read(new File(
                    "src/main/resources/img/Stable2.png"));
            stable2_dark = ImageIO.read(new File(
                    "src/main/resources/img/Stable2_dark.png"));
            lever = ImageIO.read(new File(
                    "src/main/resources/img/switch.png"));
            lever_dark = ImageIO.read(new File(
                    "src/main/resources/img/switch_dark.png"));
            tree = ImageIO.read(new File(
                    "src/main/resources/img/tree.png"));
            tree_dark = ImageIO.read(new File(
                    "src/main/resources/img/tree_dark.png"));
            blank = ImageIO.read(new File(
                    "src/main/resources/img/blank.png"));
        } catch (IOException e) {
            System.out.println(e.getStackTrace());
        }

    }
}
